package com.smart_campus.sys.service.impl;

import com.smart_campus.sys.common.*;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.domain.UserInfo;
import com.smart_campus.sys.mapper.UserMapper;
import com.smart_campus.sys.service.BandingService;
import com.smart_campus.sys.service.UserInfoService;
import com.smart_campus.sys.service.UserService;
import com.smart_campus.sys.vo.UserANDRoleANDDeptVO;
import eu.bitwalker.useragentutils.browser.SafariUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author:Omega
 * @Date:2020/7/2 10:14
 */
@Service
public class BandingServiceImpl implements BandingService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public ResultObj BandingIdentity(String username, String identity) {
        Subject subject= SecurityUtils.getSubject();
        ActiveUser activeUser= (ActiveUser) subject.getPrincipal();
        User user = activeUser.getUser();
        UserANDRoleANDDeptVO userANDRoleANDDeptVO = new UserANDRoleANDDeptVO();
        userANDRoleANDDeptVO.setUser_id(user.getUserId());
        userANDRoleANDDeptVO.setUsername(username);
        userANDRoleANDDeptVO.setUpdate_time(new Date());
        try {
            userMapper.updateUser(userANDRoleANDDeptVO);
            UserInfo userInfo = new UserInfo();
            userInfo.setIdentity(identity);
            //先判断身份证是否已注册
            UserInfo ui ;
            ui=userInfoService.selectByUserInfo(userInfo);
            try{
                if (!StringUtils.isBlank(ui.getIdentity())){
                    return ResultObj.IDENTITY_EXIST;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            //身份证和用户名是否有效
            if (!identityVerify.verification(username,identity)){
                return ResultObj.IDENTITY_FALSE;
            }
            userInfo.setUserinfoId(userInfoService.selectByUserID(user.getUserId()).getUserinfoId());
            userInfo.setIdentity(identity);
            userInfoService.updateByPrimaryKeySelective(userInfo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @Override
    public ResultObj BandingPhone(String phone, String sms) {
        //判断手机号是否被注册过
        if (!userService.queryUserByPhone(phone)) {
            return new ResultObj(-1, "该手机号已被注册！");
        }
        Subject subject= SecurityUtils.getSubject();
        ActiveUser activeUser= (ActiveUser) subject.getPrincipal();
        User user = activeUser.getUser();
        String code = redisTemplate.opsForValue().get(phone);
        try {
            if (code.equals(sms)) {
                UserANDRoleANDDeptVO UAAD = new UserANDRoleANDDeptVO();
                UAAD.setPhone(phone);
                UAAD.setUser_id(user.getUserId());
                userMapper.updateUser(UAAD);
            }
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
}
